        function showSuccessModal() {
            document.getElementById('successModal').style.display = 'flex';
        }

        // 关闭弹窗的函数
        function closeModal() {
            document.getElementById('successModal').style.display = 'none';
        }

        // 跳转到首页的函数
        function redirectToHome() {
            window.location.href = '/'; // 修改为您的首页URL
        }

        document.getElementById('passwordChangeForm').addEventListener('submit', function(e) {
            e.preventDefault();

            // 重置错误信息
            document.querySelectorAll('.error-message').forEach(el => {
                el.style.display = 'none';
            });

            // 获取表单值
            const currentPassword = document.getElementById('currentPassword').value;
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            let isValid = true;

            // 验证当前密码
            if (!currentPassword) {
                document.getElementById('currentPasswordError').textContent = '请输入当前密码';
                document.getElementById('currentPasswordError').style.display = 'block';
                isValid = false;
            }

            // 验证新密码
            if (!newPassword) {
                document.getElementById('newPasswordError').textContent = '请输入新密码';
                document.getElementById('newPasswordError').style.display = 'block';
                isValid = false;
            } else if (newPassword.length < 6) {
                document.getElementById('newPasswordError').textContent = '密码长度至少6位';
                document.getElementById('newPasswordError').style.display = 'block';
                isValid = false;
            }

            // 验证确认密码
            if (newPassword !== confirmPassword) {
                document.getElementById('confirmPasswordError').textContent = '两次输入的密码不一致';
                document.getElementById('confirmPasswordError').style.display = 'block';
                isValid = false;
            }

            if (isValid) {
                // 创建FormData对象
                const formData = new FormData();
                formData.append('current_password', currentPassword);
                formData.append('new_password', newPassword);
                formData.append('confirm_password', confirmPassword);
                formData.append('csrfmiddlewaretoken', '{{ csrf_token }}');

                // 发送请求到服务器
                fetch('/store_system/change-password/', {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // 显示成功弹窗
                        showSuccessModal();

                        // 同时显示成功消息和返回首页按钮
                        document.getElementById('successMessage').style.display = 'block';
                        document.getElementById('homeButton').style.display = 'block';
                        document.getElementById('passwordChangeForm').reset();
                    } else {
                        // 显示错误信息
                        if (data.error) {
                            if (data.error.includes('密码长度')) {
                                document.getElementById('newPasswordError').textContent = data.error;
                                document.getElementById('newPasswordError').style.display = 'block';
                            } else if (data.error.includes('当前密码')) {
                                document.getElementById('currentPasswordError').textContent = data.error;
                                document.getElementById('currentPasswordError').style.display = 'block';
                            } else if (data.error.includes('不匹配')) {
                                document.getElementById('confirmPasswordError').textContent = data.error;
                                document.getElementById('confirmPasswordError').style.display = 'block';
                            } else {
                                alert('错误: ' + data.error);
                            }
                        }
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('网络错误，请重试');
                });
            }
        });

        // 点击页面其他区域关闭弹窗
        window.addEventListener('click', function(event) {
            const modal = document.getElementById('successModal');
            if (event.target === modal) {
                closeModal();
            }
        });